package com.crl.ui.controls.pages
{
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	
	
	/**
	 *  进行翻页的tabNavigator 的皮肤文件,只做翻页;
	 * @author crl
	 * 
	 */	
	public class TabPageSkin extends Sprite
	{
		private var tabNav:TabNavigator;
		private var positions:Array;
		private var page:Page;
		private var pagecontr:PaginationControl
		private var direction:Boolean;
		private var index:int=0;
		
		private var showSize:int=0;
		private var pad:int=5;
		
		private var tabContainer:Sprite;
		private var rect:Rectangle;
		public function TabPageSkin(direction:Boolean,showSize:int=300,pad:int=5)
		{
			positions=[];
			
			this.direction=direction;
			this.pad=pad;
			this.showSize=showSize;
			
			page=new Page(1);
			page.addEventListener(Event.CHANGE,pageChangeHandler);
			pagecontr=new PaginationControl();
			pagecontr.bindPage(page);
			
			tabContainer=new Sprite();
			
			rect=new Rectangle();
			if(direction){
				rect.width=showSize;
			}else{
				rect.height=showSize;
			}
			tabContainer.scrollRect=rect; 
			this.addChild(tabContainer);
		}
		
		private function pageChangeHandler(event:Event):void{
		//	return;
			var data:Array=page.currentPageData;
			
			var pos:int=data[0];
			if(direction){
				rect.x =pos;
			}else{
				rect.y =pos;
			}
			this.tabContainer.scrollRect=rect;
		}
		
		public function clear():void{
			positions.splice(0);
		}
		
		public function addTab(tabmc:MovieClip):Boolean{
			if(tabmc==null){
				return false;
			}
			
			var index:int=tabContainer.numChildren-1;
			
			var pos:int=0;
			var child:DisplayObject;
			if(index !=-1){
				child=tabContainer.getChildAt(index);
				if(direction){
					
					var h:int=Math.max(rect.height,tabmc.height);
					if(h !=rect.height){
						rect.height=h;
						this.tabContainer.scrollRect=rect;
					}
					pos=child.x+child.width;
				}else{
				
					var w:int=Math.max(rect.width,tabmc.width);
					if(w !=rect.width){
						rect.width=w;
						this.tabContainer.scrollRect=rect;
					}
					pos=child.y+child.height;
				}
				
				pos+=pad;
			}
			
			if(direction){
				tabmc.x=pos;
			}else{
				tabmc.y=pos;
			}
			tabContainer.addChild(tabmc);
			
			positions[index]=pos;
			
			index++;
			
			page.data=positions;
			
			return true;
		}
		
		public function bindButton(prev:InteractiveObject,next:InteractiveObject):void{
			pagecontr.bindButton(prev,next);
		}
		
	}
}